﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.Database
{
    public class dbProfile
    {
        /// <summary>
        /// Gets the profile details of a user, based on the userId parameter.
        /// </summary>
        /// <param name="userId">Specifies the ID of the user to be used.</param>
        /// <returns>Returns a LoginUser object</returns>
        public static LoginUser GetProfileDetails(Guid userId)
        {
            return dbLogin.GetUserById(userId);
        }

        /// <summary>
        /// Updates the users profile information, based on the input parameters.
        /// </summary>
        /// <param name="email">Specifies the e-mail to be used in the edit.</param>
        /// <param name="firstName">Specifies the first name to be used in the edit.</param>
        /// <param name="lastName">Specifies the last name to be used in the edit.</param>
        /// <param name="phone">Specifies the phone to be used in the edit.</param>
        /// <param name="bio">Specifies the biography to be used in the edit.</param>
        /// <param name="userId">Specifies the guid of the user to be used in the edit.</param>
        public static void UpdateProfileDetails(string email, string firstName, string lastName, string phone, string bio, Guid userID)
        {
            try
            {
                MembershipUser mu = Membership.GetUser();

                if (!email.Equals(mu.Email))
                {
                    mu.Email = email;
                    Membership.UpdateUser(mu);
                }

                string connString = ConfigurationManager.ConnectionStrings["ConnStringDB"].ConnectionString;

                using (SqlConnection con = new SqlConnection(connString))
                {

                    string query = "update UserDetails set fName = @fName, lName = @lName, phone = @phone, bio = @bio where UserID = @userId";

                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.AddWithValue("@fName", firstName);

                    cmd.Parameters.AddWithValue("@lName", lastName);

                    cmd.Parameters.AddWithValue("@phone", phone);

                    cmd.Parameters.AddWithValue("@bio", bio);

                    cmd.Parameters.AddWithValue("@userId", userID);

                    con.Open();

                    cmd.ExecuteNonQuery();
                }
            }
            catch (SqlException e)
            {
                //insert logging here
                throw;
            }
            catch (Exception e)
            {
                //insert logging here
                throw;
            }
        }

        /// <summary>
        /// Gets a list of purchases, based on the userId parameter.
        /// </summary>
        /// <param name="userId">Specifies the ID of the user to which the purchases belongs to.</param>
        /// <returns>Returns a list of Purchase objects</returns>
        public static List<Purchase> GetUserPurchases(Guid userId)
        {
            return dbExchange.GetPurchasesByUserId(userId);
        }

        /// <summary>
        /// Deletes a purchase, based on the purchaseId parameter.
        /// </summary>
        /// <param name="purchaseId">Specifies the ID of the purchase to be deleted.</param>
        public static void DeleteUserPurchase(int purchaseId)
        {
            dbExchange.DeletePurchase(purchaseId);
        }

        /// <summary>
        /// Updates a purchase, based on the purchaseId parameter.
        /// </summary>
        /// <param name="title">Specifies the title to be used.</param>
        /// <param name="description">Specifies the description to be used.</param>
        /// <param name="categoryId">Specifies the ID of the category to be used.</param>
        /// <param name="isActive">Specifies whether or not the purchase is active.</param>
        /// <param name="purchaseId">Specifies the ID of the purchase to be updated.</param>
        public static void UpdateUserPurchase(string title, string description, int categoryId, bool isActive, int purchaseId)
        {
            dbExchange.UpdatePurchase(title, description, categoryId, isActive, purchaseId);
        }

        /// <summary>
        /// Gets a list of sales, based on the userId parameter.
        /// </summary>
        /// <param name="userId">Specifies the ID of the user to which the sales belongs to.</param>
        /// <returns>Returns a list of Sale objects</returns>
        public static List<Sale> GetUserSales(Guid userId)
        {
            return dbExchange.GetSalesByUserId(userId);
        }

        /// <summary>
        /// Deletes a sale, based on the saleId parameter.
        /// </summary>
        /// <param name="saleId">Specifies the ID of the sale to be deleted.</param>
        public static void DeleteUserSale(int saleId)
        {
            dbExchange.DeleteSale(saleId);
        }

        /// <summary>
        /// Updates a sale, based on the saleId parameter.
        /// </summary>
        /// <param name="title">Specifies the title to be used.</param>
        /// <param name="description">Specifies the description to be used.</param>
        /// <param name="categoryId">Specifies the ID of the category to be used.</param>
        /// <param name="isActive">Specifies whether or not the sale is active.</param>
        /// <param name="saleId">Specifies the ID of the sale to be updated.</param>
        public static void UpdateUserSale(string title, string description, int categoryId, bool isActive, int saleId)
        {
            dbExchange.UpdateSale(title, description, categoryId, isActive, saleId);
        }
    }
}